package com.cloudptt.api.product.parser;

import android.content.Context;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.ErrInfo;
import com.cloudptt.api.models.ExtStrInfo;
import com.cloudptt.api.pjsip.api.SipCallSession;
import com.cloudptt.api.product.log.Log;

public class ErrParser {

	public final static String THIS_FILE = "ErrParser";
	
	public static final int Type_CallResp = 1;
	public static final int Type_FloorReq = 2;
	public static final int Type_FloorResp = 3;
	public static final int Type_FloorRelResp =  4;

	private String errStr = "";
	private boolean isErr = false;
	
	private Context context = null;
	
	public ErrParser(int code, int type,Context ctx)
	{
		this.context = ctx;
//		 = callSession.getLastStatusCode();
//		String codeText = callSession.getLastStatusComment();
		
		Log.e(THIS_FILE, "ErrParser code is " + code + " type is " + type + " code = "+ code);
		
		if(code >= CauseCont.SipErrCode_Begin)//正常错误流程由606或者486消息携带
		{
			isErr = true;
//			if(code == CauseCont.SipErrCode_Init_606
//					|| code == CauseCont.SipErrCode_Init_486)
//			{
////				String extStr = callSession.getErrExtStr(type);//.getRespExtStr();
////				errStr = getDefineErrStr(extStr);
//			}
//			else
			{
				errStr = getSipErrStr(code);
			}
		}
		else if(type == Type_FloorReq || type == Type_FloorResp )//强制释放话权消息 是通过reinvite消息携带扩展
		{
			isErr = true;
			
//			String extStr = callSession.getErrExtStr(type);//.getRespExtStr();
//			errStr = getDefineErrStr(extStr);
//			if(type == Type_FloorReq)
//			{
//				callSession.initFloorReqExtStr();
//			}
		}
	}
	
	public String getErrStr()
	{
		return errStr;
	}
	
	public boolean isErr() {
		return isErr;
	}
	
	@SuppressWarnings("unused")
	private String getSipErrStr(int code)
	{
		ErrInfo errInfo = new ErrInfo(CauseCont.ERROR_TITLE_SIP, code);
		if( errInfo != null )
		{
			return errInfo.getErrDesc();
		}
		return "";
	}
	
	private String getDefineErrStr(String extStr)
	{
		Log.d(THIS_FILE, "getDefineErrStr extStr is " + extStr);
		ExtStrInfo extStrInfo= new ExtStrInfo(extStr);
		return extStrInfo.getErrDesc(context);
	}
}
